﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Web.Routing;

namespace MvcCarConsume.WebUI
{
    public static class PagerHelpers
    {
        private const int range = 2;
        public static MvcHtmlString PageLinks(this HtmlHelper html,
                                        VMPageInfo pagingInfo,
                                        Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 1; i <= pagingInfo.TotalPage; i++)
            {
                if (i != pagingInfo.CurrentPage)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.InnerHtml = i.ToString();
                    //if (i == pagingInfo.CurrentPage)
                    //    tag.AddCssClass("selected");
                    result.AppendLine(tag.ToString());
                }
                else
                    result.AppendLine(i.ToString());
            }

            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString PageLinks2(this HtmlHelper html, VMPageInfo pagingInfo, Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            int total = pagingInfo.TotalPage;
            bool previewdot = false;
            bool nextdot = false;
            for (int i = 1; i <= total ; i++)
            {
                //range = 2 for static
                if (i == 1 || (i > pagingInfo.CurrentPage - range && i < pagingInfo.CurrentPage + range) || i == total)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.InnerHtml = i.ToString();
                    if (i == pagingInfo.CurrentPage)
                        tag.AddCssClass("selectedpage");
                    result.AppendLine(tag.ToString());
                }
                else
                {
                    if(i > pagingInfo.CurrentPage - range && previewdot == false)
                    {
                        result.AppendLine("...");
                        previewdot = true;
                    }
                    if (i < pagingInfo.CurrentPage + range && nextdot == false)
                    {
                        result.AppendLine("...");
                        nextdot = true;
                    }
                }
            }

            return MvcHtmlString.Create(result.ToString());
        }

    }
}